<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://esm.sh/@testduet/wait-for"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';
      import renderHook from '../../hooks/private/renderHook.js';

      run(async function () {
        const {
          React: { createElement },
          testHelpers: { createDirectLineEmulator },
          WebChat: {
            Components: { BasicWebChat, Composer },
            hooks: { useSuggestedActions },
            testIds
          }
        } = window;

        const { directLine, store } = createDirectLineEmulator();

        // WHEN: Render initially.
        const renderResult = renderHook(() => useSuggestedActions(), {
          legacyRoot: true,
          wrapper: ({ children }) =>
            createElement(Composer, { directLine, store }, createElement(BasicWebChat), children)
        });

        await pageConditions.uiConnected();

        // WHEN: Dispatching "WEB_CHAT/SET_SUGGESTED_ACTIONS" action.
        store.dispatch({
          payload: {
            suggestedActions: [
              { title: 'Hello, World!', type: 'imBack' },
              { title: 'Aloha!', type: 'imBack' }
            ]
          },
          type: 'WEB_CHAT/SET_SUGGESTED_ACTIONS'
        });

        // THEN: Should not show activity.
        expect(pageElements.activities()).toHaveLength(0);

        // THEN: Should show 2 suggested actions.
        await waitFor(() => expect(pageElements.allByTestId(testIds.suggestedActionButton)).toHaveLength(2));
        expect(pageElements.allByTestId(testIds.suggestedActionButton)[0]).toHaveProperty(
          'textContent',
          'Hello, World!'
        );
        expect(pageElements.allByTestId(testIds.suggestedActionButton)[1]).toHaveProperty('textContent', 'Aloha!');

        // THEN: getState() should have 2 suggested actions.
        expect(store.getState().suggestedActions).toHaveLength(2);
        expect(store.getState().suggestedActions[0]).toEqual({ title: 'Hello, World!', type: 'imBack' });
        expect(store.getState().suggestedActions[1]).toEqual({ title: 'Aloha!', type: 'imBack' });

        // THEN: useSuggestedActions() should have 2 suggested actions.
        renderResult.rerender();
        expect(renderResult).toHaveProperty('result.current', [
          [
            { title: 'Hello, World!', type: 'imBack' },
            { title: 'Aloha!', type: 'imBack' }
          ],
          expect.any(Function),
          { activity: undefined }
        ]);
      });
    </script>
  </body>
</html>
